import axios from "axios";
import { Notification } from "element-ui";

const BASE_URL = "/openSciBeijingApi";

axios.defaults.baseURL = BASE_URL;

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    if (response.status >= 200 && response.status < 300) {
        return response?.data;
    }
    Notification.error(response.statusText);
    return Promise.reject(response);
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    Notification.error(error.message);
    return Promise.reject(error);
});

const get = (url, params = {}) => {
    return axios.get(url, { params });
}

const post = (url, data = {}, headers = {
    'Content-Type': 'application/json'
}) => {
    return axios.post(url, data, headers);
}

const request = axios

export { get, post }
export default request;

